package com.xinshuyuan.xinshuyuanworkandexercise.Model.Keybord;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;

import com.xinshuyuan.xinshuyuanworkandexercise.R;

import java.util.List;

/**
 * Created by Administrator on 2017/7/14.
 */

public class KeyboardUtil{
    private Context ctx;
    private Activity act;
    private KeyboardView keyboardView;
    private Keyboard k1;// 字母键盘
    private Keyboard k2;// 数字键盘
    public boolean isnun = false;// 是否数据键盘
    public boolean isupper = false;// 是否大写
    private LinearLayout lii;

    private EditText ed;

    public KeyboardUtil(Activity act, Context ctx, EditText edit,int keyboardViewId, LinearLayout ikk) {

        this.lii=ikk;
        this.act = act;
        this.ctx = ctx;
        this.ed = edit;
        if(ikk.getVisibility()==View.GONE){
            ikk.setVisibility(View.VISIBLE);
        }
        k1 = new Keyboard(ctx, R.xml.qwerty);
        k2 = new Keyboard(ctx, R.xml.symbols);
        keyboardView = (KeyboardView) act.findViewById(keyboardViewId/*R.id.keyboard_view*/);
        keyboardView.setKeyboard(k1);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(listener);
    }

    private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
        @Override
        public void swipeUp() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = ed.getText();
            int start = ed.getSelectionStart();
            if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
//				ed.clearFocus();
//		        ed.setFocusable(false);
                hideKeyboard();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                if (editable != null && editable.length() > 0) {
                    if (start > 0) {
                        editable.delete(start - 1, start);
                    }
                }
            } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小写切换
                changeKey();
                keyboardView.setKeyboard(k1);

            } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 数字键盘切换
                if (isnun) {
                    isnun = false;
                    keyboardView.setKeyboard(k1);
                } else {
                    isnun = true;
                    keyboardView.setKeyboard(k2);
                }
            } else if (primaryCode == 57419) { // go left
                if (start > 0) {
                    ed.setSelection(start - 1);
                }
            } else if (primaryCode == 57421) { // go right
                if (start < ed.length()) {
                    ed.setSelection(start + 1);
                }
            }else if (primaryCode == 4001) { // +
                editable.insert(start,  "＋");
            } else if (primaryCode == 4002) { // -
                editable.insert(start,  "-");
            } else if (primaryCode == 4003) { // ×
                editable.insert(start,  "×");
            } else if (primaryCode == 4004) { // ÷
                editable.insert(start,  "÷");
            }else if (primaryCode == 4005) { // =
                editable.insert(start,  "=");
            }else if (primaryCode == 4006) { // ≠
                editable.insert(start,  "≠");
            }else if (primaryCode == 3003) { // ✓
                editable.insert(start,  "✓");
            }else if (primaryCode == 3004) { // ✕
                editable.insert(start,  "✕");
            }else if (primaryCode == 5001) { // >
                editable.insert(start,  ">");
            }else if (primaryCode == 5002) { // ≥
                editable.insert(start,  "≥");
            }else if (primaryCode == 5003) { // ＜
                editable.insert(start,  "＜");
            }else if (primaryCode == 5004) { //≤
                editable.insert(start,  "≤");
            } else {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    };

    /**
     * 键盘大小写切换
     */
    private void changeKey() {
        List<Keyboard.Key> keylist = k1.getKeys();
        if (isupper) {//大写切换小写
            isupper = false;
            for(Keyboard.Key key:keylist){
                if (key.label!=null && isword(key.label.toString())) {
                    key.label = key.label.toString().toLowerCase();
                    key.codes[0] = key.codes[0]+32;
                }
            }
        } else {//小写切换大写
            isupper = true;
            for(Keyboard.Key key:keylist){
                if (key.label!=null && isword(key.label.toString())) {
                    key.label = key.label.toString().toUpperCase();
                    key.codes[0] = key.codes[0]-32;
                }
            }
        }
    }

    public void showKeyboard() {

        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    public void hideKeyboard() {
        int visibility = lii.getVisibility();
        if (visibility == View.VISIBLE) {
            lii.setVisibility(View.GONE);

        }
    }

    private boolean isword(String str){
        String wordstr = "abcdefghijklmnopqrstuvwxyz";
        if (wordstr.indexOf(str.toLowerCase())>-1) {
            return true;
        }
        return false;
    }

}
